package com.szt.sy.util.encrypt.shiro;

/***
 * sha-1加密 用于shiro密码加密
 *
 * @author kjq
 *
 */
public class Sha1ShiroEncrypt {
	/***
	 * 算法名称
	 */
	private static String algorithName = "SHA-1";
	/***
	 * hash迭代次数
	 */
	private static int hashIterations = 1024;
	/***
	 * salt的长度
	 */
	private static int saltLength = 8;
	
	private Sha1ShiroEncrypt() {
	}
	
	public static PasswordInfo encryptPassword(String password) {
		PasswordInfo info = new PasswordInfo();
		byte[] salt = Digests.generateSalt(saltLength);
		info.setSalt(Encodes.encodeHex(salt));
		byte[] hashPassword = Digests.sha1(password.getBytes(), salt, hashIterations);
		info.setPassword(Encodes.encodeHex(hashPassword));
		return info;
	}
	
	public static String encryptPasswordBySalt(String password, String salt) {
		byte[] bSalt = Encodes.decodeHex(salt);
		byte[] hashPassword = Digests.sha1(password.getBytes(), bSalt, hashIterations);
		return Encodes.encodeHex(hashPassword);
	}
	
	/***
	 * 判断密码是否正确
	 *
	 * @param password
	 *            用户输入的密码
	 * @param salt
	 *            数据库里面的密码的盐
	 * @param encrptPassword
	 *            数据库里面的密码
	 * @return
	 */
	public static boolean isPasswordTrue(String password, String salt, String encrptPassword) {
		String ePassword = encryptPasswordBySalt(password, salt);
		if (encrptPassword.equals(ePassword)) {
			return true;
		} else {
			return false;
		}
	}
	
}
